<?php
namespace app\adminapiv3\service;

/**
 * @name: 系统伪装
 * @Param: 
 * @Description: 
 * @Author: foo
 * @Date: 2019-10-31 16:37:06
 */
use app\common\service;
class SystemMask extends Base
{
    /**
     * @name: 开启
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 16:37:25
     */
    function on ($data)
    {
        $HostLink = new service\HostLink();
        $HostLink->toDo('checkOnline', ['disguise']);

        $host_id_a = $this->getHost('id');
        $HostGuiseConfig = new service\HostGuiseConfig();
        $HostGuiseConfig->toDo('onOff', [$host_id_a, 1]);

        $this->toDo('configModify', [$data]);
    }

    /**
     * @name: 关闭
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 16:37:39
     */
    function off ()
    {
        $HostLink = new service\HostLink();
        $HostLink->toDo('checkOnline', ['disguise']);

        $host_id_a = $this->getHost('id');
        $HostGuiseConfig = new service\HostGuiseConfig();
        $HostGuiseConfig->toDo('onOff', [$host_id_a, 0]);
    }

    /**
     * @name: 配置修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 10:30:53
     */
    function configModify ($data)
    {
        $host_id_a = $this->getHost('id');
        $HostGuisePort = new service\HostGuisePort();

        $insertData = [];
        foreach ($host_id_a as $host_id)
        {
            foreach ($data as $k => $v)
            {
                $va = explode(',', $v);
                foreach ($va as $cv  )
                {
                    if(!empty($cv))
                    {
                        $insertData[] = [
                            'host_id' => $host_id,
                            'port' => $cv,
                            'protocols' => $k == 'udp' ? 2 : 1 ,
                            'status' => 1,
                            'type' => $k
                        ];
                       $checkPort[$host_id][] = $cv;
                    }
                    
                }
            }
        }

        $HostPort = new service\HostPort();
        $HostPort->toDo('checkPort', [$checkPort]);
        $HostGuisePort->toDo('delete', [$host_id_a]);//删除
        $HostGuisePort->toDo('add', [$insertData]);//添加
    }

    /**
     * @name: 设置详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 10:45:18
     */
    function configInfo ($host_id)
    {
        $HostGuisePort = new service\HostGuisePort();
        $data = $HostGuisePort->toDo('info', [$host_id]);
        $port_a =[];
        foreach ($data as $port)
        {
            $port_a[$port['type']][] = $port['port'];
        }

        $return = [
            'middleware' => 0,
            'sql' => 0,
            'standard' => 0,
            'tcp' => '',
            'udp' => '',
        ];

        foreach ($port_a as $k => $v)
        {
            $return[$k] = implode(',', $v);
        }

        return $return;
    }

    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 11:23:17
     */
    function list ($filterData, $limit)
    {
        $HostGuiseConfig = new service\HostGuiseConfig();
        $config = $HostGuiseConfig->findOne($filterData['host_id']);
        if ($config['guise_status'] == 0)
        {
            return [];
        }

        $HostGuisePort = new service\HostGuisePort();

        $return = $HostGuisePort->toDo('list', [$filterData, $limit]);

        return $return;
    }

}